package io.github.wjehovah.yyn.activity.profile;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import io.github.wjehovah.yyn.R;
import io.github.wjehovah.yyn.adapter.TypedListAdapter;
import io.github.wjehovah.yyn.bean.CommBean;
import io.github.wjehovah.yyn.bean.ProfileBean;
import io.github.wjehovah.yyn.data.Budejie;
import io.github.wjehovah.yyn.utils.Engine;
import io.github.wjehovah.yyn.view.XListView;

public class PersonalProfileActivity extends Activity implements XListView.IXListViewListener, View.OnClickListener {
    @ViewInject(R.id.list_profile)
    private XListView mListView;
    private TypedListAdapter mAdapter;
    private HttpUtils httpUtils;
    private CommBean.BeanDetail beanDetail;//点击传过来的数据
    private ProfileBean profileBean;//profile 数据
    private Gson gson;
    private List<CommBean.BeanDetail> data;//listView 数据
    private LayoutInflater mLayoutInflater;
    private BitmapUtils bitmapUtils;

    @ViewInject(R.id.profile_content_container)
    private LinearLayout container;
    @ViewInject(R.id.profile_name)
    private TextView textView;
    @ViewInject(R.id.title_left_btn)
    private Button back;
    @ViewInject(R.id.aiv_pp_portrait)
    private ImageView userIcon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        mLayoutInflater = LayoutInflater.from(this);
        View view = mLayoutInflater.inflate(R.layout.activity_personal_profile_new, null, false);
        //View header = mLayoutInflater.inflate(R.layout.personal_profile_head, null, false);
        //  container.addView(header, 1);
        ViewUtils.inject(this, view);
        setContentView(view);
        bitmapUtils = new BitmapUtils(this);
        Intent intent = getIntent();
        beanDetail = intent.getParcelableExtra(Budejie.PROFILE);
        textView.setText(beanDetail.screen_name);
        bitmapUtils.display(userIcon,beanDetail.profile_image);
        back.setOnClickListener(this);


        httpUtils = new HttpUtils(2000);
        gson = new Gson();
        data = new ArrayList<>();
        String url = urlCreate();
        requestData(url);


    }

    private void requestData(String url) {

        httpUtils.send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String json = responseInfo.result;
                Log.e(Budejie.TAG + "profile", json);
                ProfileBean bean = parseJson(json);
                profileBean = bean;//更新数据
                data.addAll(bean.topics);
                initView();
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                onLoadFinish();
            }
        });
    }

    private ProfileBean parseJson(String json) {
        return gson.fromJson(json, ProfileBean.class);

    }

    private void initView() {
        if (mAdapter == null) {
            mAdapter = new TypedListAdapter(this, data);
            mListView.setAdapter(mAdapter);
            mListView.setPullLoadEnable(true);
            mListView.setPullRefreshEnable(false);
            mListView.setXListViewListener(this);
        } else {
            onLoadFinish();
            mAdapter.notifyDataSetChanged();
        }
    }

    private String urlCreate() {
        //profile 要拼接 url  &lastid= &visit_uid=
        return Budejie.PERSONAL_PROFILE_URL + "&visit_uid=" + beanDetail.user_id + "&lastid=";
    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onLoadMore() {
        if (Engine.Http.getAliveNetWork()) {//请求
            loadMore();//上拉加载更多数据
        } else {
            onLoadFinish();
        }
    }

    private void loadMore() {
        String url = urlCreate() + profileBean.last_id;
        Log.e(Budejie.TAG, url);
        requestData(url);
    }

    private void onLoadFinish() {
        mListView.stopRefresh();
        mListView.stopLoadMore();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_left_btn:
                finish();
                break;
        }
    }
}
